class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        dfs(root,0);
        return res;
    }
    
    void dfs(TreeNode* root,int depth)
    {
        if(!root)
            return;
        if(depth>=res.size())
            res.resize(depth+1,INT_MIN);
        res[depth]=max(res[depth],root->val);
        dfs(root->left,depth+1);
        dfs(root->right,depth+1);
    }

private:
    vector<int> res;
};